package com.company.example.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.company.example.service.MongoDBService;

/**
 * @package   com.company.example.core.common
 * @file MongoDBBaseDao.java
 * @author   yanwl
 * @date 2016年9月29日
 * @version V 1.0
 */
//@Service("mongoDBService")
public class MongoDBServiceImpl implements MongoDBService{
	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public <T> T findById(Class<T> entityClass, String id) {
		return this.mongoTemplate.findById(id, entityClass);
	}

	@Override
	public <T> List<T> findAll(Class<T> entityClass) {
		return this.mongoTemplate.findAll(entityClass);
	}

	@Override
	public void delete(Object object) {
		this.mongoTemplate.remove(object);
	}

	@Override
	public void insert(Object object) {
		this.mongoTemplate.insert(object);
	}

	@Override
	public void saveOrUpdate(Object object) {
		this.mongoTemplate.save(object);
	}
	
	public MongoTemplate getMongoTemplate() {
		return this.mongoTemplate;
	}

	@Override
	public void batchSave(List<Object> list) {
		this.mongoTemplate.insertAll(list);
	}

	@Override
	public <T> long count(Query query, Class<T> entityClass) {
		return this.mongoTemplate.count(query, entityClass);
	}

	@Override
	public <T> List<T> find(Query query, Class<T> entityClass) {
		return this.mongoTemplate.find(query, entityClass);
	}

	@Override
	public <T> void update(Query query, Update update, Class<T> entityClass) {
		this.mongoTemplate.updateFirst(query, update, entityClass);
	}
}
